/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.modules.repair.dto.PlanWorkItemWatermarkDTO;
import org.springblade.modules.repair.entity.MaintainWatermarkImg;
import org.springblade.modules.repair.utils.WatermarkImgUtils;
import org.springblade.modules.repair.vo.MaintainWatermarkImgVO;
import org.springblade.modules.repair.mapper.MaintainWatermarkImgMapper;
import org.springblade.modules.repair.service.IMaintainWatermarkImgService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 维保相关水印照片表 服务实现类
 *
 * @author ysq
 * @since 2023-10-24
 */
@Service
public class MaintainWatermarkImgServiceImpl extends BaseServiceImpl<MaintainWatermarkImgMapper, MaintainWatermarkImg> implements IMaintainWatermarkImgService {

	@Override
	public IPage<MaintainWatermarkImgVO> selectMaintainWatermarkImgPage(IPage<MaintainWatermarkImgVO> page, MaintainWatermarkImgVO maintainWatermarkImg) {
		return page.setRecords(baseMapper.selectMaintainWatermarkImgPage(page, maintainWatermarkImg));
	}

	@Override
	public Boolean updateWatermarkByImgId(List<String> imgUrlList) {
		if(Func.isEmpty(imgUrlList)){
			throw new ServiceException("图片数据不能为空");
		}
		List<String> imgId = imgUrlList.stream().map(WatermarkImgUtils::subImgNameNotSuffix).collect(Collectors.toList());
		List<MaintainWatermarkImg> imgList = baseMapper.selectList(Wrappers.<MaintainWatermarkImg>lambdaQuery().in(MaintainWatermarkImg::getImgId,imgId));
		if(Func.isNotEmpty(imgList)){
			imgList.forEach(e -> imgUrlList.forEach(j -> {
				if(WatermarkImgUtils.subImgNameNotSuffix(j).equals(e.getImgId())){
					e.setImgUrl(j);
				}
			}));
			return super.updateBatchById(imgList);
		}
		return Boolean.TRUE;
	}

	@Override
	public Boolean updatePlanWatermark(PlanWorkItemWatermarkDTO dto) {
		if(ObjectUtil.isEmpty(dto.getImgList())){
			return super.update(Wrappers.<MaintainWatermarkImg>lambdaUpdate().set(MaintainWatermarkImg::getStatus,1).eq(MaintainWatermarkImg::getPlanId,dto.getPlanId())
				.eq(MaintainWatermarkImg::getPlanWorkItemId,dto.getPlanWorkItemId()));
		}
		List<String> imgIdList = dto.getImgUrlList().stream().map(WatermarkImgUtils::subImgNameNotSuffix).collect(Collectors.toList());
		List<MaintainWatermarkImg> watermarkImgList = baseMapper.selectList(Wrappers.<MaintainWatermarkImg>lambdaQuery().eq(MaintainWatermarkImg::getPlanId,dto.getPlanId())
			.eq(MaintainWatermarkImg::getPlanWorkItemId,dto.getPlanWorkItemId()).in(MaintainWatermarkImg::getImgId,imgIdList));
		if(ObjectUtil.isNotEmpty(watermarkImgList)){
			// 清除不存在图片的数据
			super.update(Wrappers.<MaintainWatermarkImg>lambdaUpdate().set(MaintainWatermarkImg::getStatus,1).eq(MaintainWatermarkImg::getPlanId,dto.getPlanId())
				.eq(MaintainWatermarkImg::getPlanWorkItemId,dto.getPlanWorkItemId()).notIn(MaintainWatermarkImg::getId,watermarkImgList.stream().map(MaintainWatermarkImg::getId).collect(Collectors.toList())));

			watermarkImgList.forEach(e -> dto.getImgUrlList().forEach(j -> {
				e.setPlanWorkItemId(dto.getPlanWorkItemId());
				e.setImgUploadAddress(dto.getImgUploadAddress());
				e.setWatermarkType(dto.getWatermarkType());
				e.setItemType(dto.getItemType());
				e.setStatus(2);
				if(WatermarkImgUtils.subImgNameNotSuffix(j).equals(e.getImgId())){
					e.setImgUrl(j);
				}
			}));
			return super.updateBatchById(watermarkImgList);
		}

		return Boolean.TRUE;
	}

	@Override
	public Boolean updatePlanWorkItemWatermark(PlanWorkItemWatermarkDTO dto) {
		List<MaintainWatermarkImg> oldImgList = super.list(Wrappers.<MaintainWatermarkImg>lambdaQuery().eq(MaintainWatermarkImg::getPlanId,dto.getPlanId())
			.eq(MaintainWatermarkImg::getPlanWorkItemId,dto.getPlanWorkItemId()));
		if(Func.isEmpty(dto.getImgList())){
			return super.update(Wrappers.<MaintainWatermarkImg>lambdaUpdate().set(MaintainWatermarkImg::getStatus,1).eq(MaintainWatermarkImg::getPlanId,dto.getPlanId())
				.eq(MaintainWatermarkImg::getPlanWorkItemId,dto.getPlanWorkItemId()));
		}
		if(Func.isEmpty(oldImgList) && Func.isNotEmpty(dto.getImgList())){
			return super.saveBatch(dto.getImgList());
		}
		if(Func.isNotEmpty(oldImgList)){
			List<String> oldImgIdList = oldImgList.stream().map(MaintainWatermarkImg::getImgId).collect(Collectors.toList());
			dto.getImgList().forEach(e -> {
				if(!oldImgIdList.contains(WatermarkImgUtils.subImgNameNotSuffix(e.getImgUrl()))){
					oldImgList.add(e);
				}
			});
			List<String> imgIdList = dto.getImgList().stream().map(e -> WatermarkImgUtils.subImgNameNotSuffix(e.getImgUrl())).collect(Collectors.toList());
			oldImgList.forEach(e -> {
				if(!imgIdList.contains(e.getImgId())){
					e.setStatus(1);
				}
			});
			return super.saveOrUpdateBatch(oldImgList);
		}
		return Boolean.TRUE;
	}

}
